<template>
	<view class="scrool-page">
		<!-- <view class="header-cont"></view> -->
		<view class="list-cont">
			<scroll-view  class="scrool-more" style="height: 100%" scroll-y="true" scroll-with-animation="true">
				<view v-for="(item,index) in productList" :key="index">
					<uni-card :title="item.custName" @click="showDetail(item)" v-if="item.attr2 == '10'" style='background-color: darkgrey;'>
						<text class="content">{{ item.panelCode }}</text>						
						<text class="content" style="margin-left: 20px;">{{item.boxNum}}箱</text><br>
						<text class="uni-body">是否完成:{{item.attr2=='10' ? '已完成' : '未完成'}}</text><br>
						<view v-for="(child,index) in item.itemCodeSet" :key="index">
							<text class="uni-body">物料编码：{{ child.itemCode }}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{child.itemBoxNum}}箱</text><br>
						</view>						
					</uni-card>
					
					<uni-card :title="item.custName" @click="showDetail(item)" v-else>
						<text class="content">{{ item.panelCode }}</text>						
						<text class="content" style="margin-left: 20px;">{{item.boxNum}}箱</text><br>
						<text class="uni-body">是否完成:{{item.attr2=='10' ? '已完成' : '未完成'}}</text><br>
						<view v-for="(child,index) in item.itemCodeSet" :key="index">
							<text class="uni-body">物料编码：{{ child.itemCode }}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{child.itemBoxNum}}箱</text><br>
						</view>						
					</uni-card>
				</view>				
			</scroll-view>			
		</view>
		
		<view class="footer">
			<button type="default"  @click="sumbit" >提交</button>
		</view>
	</view>
</template>

<script>
	import { getToken } from '@/utils/auth';
	export default {
		data() {
			return {
				productList:[],
				showList:[],
				delay:800,
				so_docno:'',
				status:'',
				title:'',
				id:'',
				baseUrl:getApp().globalData.config.baseUrl,
				userName:this.$store.state.user.name,
				orgId:getApp().globalData.config.orgId,
			}
		},
		
		onLoad: function (option) { //option为object类型，会序列化上个页面传递的参数
			this.orgId = uni.getStorageSync('orgId');
			this.so_docno = option.pkOrder;
			//this.so_docno = '6041760172263342080';
			this.status = option.status;
			this.title = option.title;
			this.id = option.id;
			uni.setNavigationBarTitle({
				title:this.title
			});
			
		},
		
		mounted() {
			this.init();
		},
		
		// created() {
		// 	this.orgId = uni.getStorageSync('orgId');
		// 	this.init()
		// },
		
		methods: {
			init(){	
				//显示加载框
				uni.showLoading({
					title: '加载中',
					mask:true
				});
				var that = this;
				let token = getToken();
				uni.request({
					//url:that.baseUrl+'/system/pkgplan/select/vitoplan',
					// url:that.baseUrl+"/system/pkgplan/recommend",
					
					 url:that.baseUrl+"/system/pkgplan/recommend/boxs",
					data:{
						packageId : that.so_docno,
						orgId:that.orgId,
						userName:that.userName,
						valueName:'warehouse',//字典,
						id:that.id,
						title:that.title
					},
					method: 'POST',
					header:{
						'authorization': token
					},
					dataType:'json',
					success: (res) =>{
						console.log(res.data);
						if(res.data.code =='200'){
							var result = res.data.data.productList;
							if(result == null || result == '' || result == '{}' || result.length == 0){
								//隐藏加载框
								uni.hideLoading();
								uni.showToast({
									title: '无数据',
									icon: 'none',
									duration: 3000
								});
								return;
							}
							//this.productList = res.data.data.productList;
							that.productList = [];
							that.delayloadDp(res.data.data.productList,5,0,that.delay,that.productList);
							//隐藏加载框
							uni.hideLoading();
						}else if(res.data.code=='50000'){//20240205判断组托计划锁定
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 5000
							});
						}else{
							//隐藏加载框
							uni.hideLoading();
							uni.showToast({
								title: '查询出错',
								icon: 'none',
								duration: 3000
							});
						}						
					}
				})
			},
			
			sumbit(){
				//显示加载框
				uni.showLoading({
				    title: '加载中',
					mask:true
				});
				if(this.status =='2'){
					//隐藏加载框
					uni.hideLoading();
					uni.showToast({
						title: '任务已挂起，请先恢复',
						icon: 'none',
						duration: 3000
					});
					return;
				}
				
				if(this.status =='3'){
					//隐藏加载框
					uni.hideLoading();
					uni.showToast({
						title: '任务已完成，不能修改',
						icon: 'none',
						duration: 3000
					});
					return;
				}
				//后期加一个校验是否完成，完成就不能修改了
				
				for(var i=0;i<this.productList.length;i++){
					var childBoxNum = 0;
					var data = this.productList[i];
					if(data.attr2 != 10){
						uni.hideLoading();
						uni.showToast({
							title: '托号'+data.panelCode+'尚未完成，请检查',
							icon: 'none',
							duration: 2000
						});
						return;
					}
				}
				
				uni.request({
					url:this.baseUrl+'/system/pkgplan/status',
					data:{
						packageId : this.so_docno,
						orgId:this.orgId,
						userName:this.userName,
						id:this.id,
						type:'app'
					},
					method: 'POST',
					header:{
						'authorization': getToken()
					},
					dataType:'json',
					success: (res) =>{
						//隐藏加载框
						uni.hideLoading();
						if(res.data.code == '200'){
							uni.showToast({
								title: res.data.msg,
								icon: 'success',
								duration: 3000
							});
							let pages = getCurrentPages();  //获取所有页面栈实例列表
							let nowPage = pages[ pages.length - 1];  //当前页页面实例
							let prevPage = pages[ pages.length - 2 ];  //上一页页面实例							
							setTimeout(function(){
								uni.navigateBack({  //uni.navigateTo跳转的返回，默认1为返回上一级
									delta: 1
								});
							},1000);
						}else{
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 3000
							});
						}
					}
				})
			},
			
			showDetail(item){
				//显示加载框
				uni.showLoading({
				    title: '加载中',
					mask:true
				});
				item.childBoxNum = 0;
				//var childList = encodeURIComponent(JSON.stringify(item.childList));
				var itemCodeSet = encodeURIComponent(JSON.stringify(item.itemCodeSet));
				//var needAdd = encodeURIComponent(JSON.stringify(item.needAdd));
				//var url = '/pages/wm_package/wm_package_detail?detailList='+childList+"&needAdd="+needAdd+"&custName="+item.custName+"&title="+this.title
				//	+"&packageId="+item.packageId+"&custCode="+item.custCode+"&panelCode="+item.panelCode+"&boxNum="+item.boxNum+"&status="+this.status;
				var url = '/pages/wm_package/wm_package_detail?itemCodeSet='+itemCodeSet+"&custName="+item.custName+"&title="+this.title
					+"&packageId="+item.packageId+"&custCode="+item.custCode+"&panelCode="+item.panelCode+"&boxNum="+item.boxNum
					+"&status="+this.status+"&fullContaine="+item.fullContaine+"&postNum="+item.postNum+"&boxName="+item.boxName+"&boxQty="+item.boxQty;
				//隐藏加载框
				uni.hideLoading();
				uni.navigateTo({
					url: url
				});
			},
			
			delayloadDp(list, pageSize, current, delay,showList) {
				//延时加载
				if (current != 0) {
					current++;
				}
				for (let i = current; i < list.length; i++) {
					showList.push(list[i]);
					current = i;
					if (i != 0 && i % pageSize == 0) {
						break;
					}
				}
				if (showList.length == list.length) {
					return;
				}
				setTimeout(() => {
					this.delayloadDp(list, pageSize, current, delay,showList)
				}, 200 + delay)
			},
		}
	}
</script>

<style lang="less">
	 page {
		width: 100%;
		height: 100%;
	 }
	 
	 
	 .scrool-page {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100%;
	 }
	 
	 .header-cont {
		width: 100%;
		height: 110rpx;
		line-height: 100rpx;
		text-align: center;
		// background: red;
	 }
 
	 .list-cont {
		flex: 1;
		position: relative;
 
		.scrool-more {
			position: absolute;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
		}
	 
		.list {
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background: #e0e0e0;
	 
		}
	}
 
 
	 .footer {
		color: #fff;
		line-height: 100rpx;
		flex: 0 0 100rpx;
		/* 不放大不缩小固定100rpx */
	 }
	 
	 .content{
		 font-size: 38upx;
	 }
 </style>
